<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/validate.jsp"%>
<%
	String id = input.getDesc("template_id", "-1");
	List<Map> templates = QBuilder.db("basedb").select("new map(id as template_id, template_name as title, template_type as detail)").from("visit_template").where(new Where("id =", id).and("status = ", STATUS_ENABLE)).query().getResultObj();
	if (isEmpty(templates)) {
		msg = new Message(30041, "暂无随访模板");
		Tools.encryptPrint(out, msg);
		return;
	}
	Map template = templates.get(0);
	String templateId = Tools.getDesc(template.get("template_id"), "-1");
	// 查询节点
	List<Map> points = QBuilder.db("basedb").select("new map(id as point_id, title as title)").from("visit_point").where(new Where("template_id =", templateId).and("status = ", STATUS_ENABLE)).order("show_order", QBuilder.asc).query().getResultObj();
	template.put("points", points);
	if (!isEmpty(points)) {
		String pointIds = "-1";
		for (int i = 0; i < points.size(); i++) {
			Map point = points.get(i);
			pointIds += "," + Tools.getDesc(point.get("point_id"), "-1");
		}
		List<Map> surveys = QBuilder.db("basedb").select("new map(s.id as survey_id, s.title as title, vpl.link_type as link_type, vpl.point_id as point_id)").from("visit_point_link vpl, Survey s").where(new Where("vpl.link_type = 0").and("vpl.status = ", STATUS_ENABLE).and("vpl.point_id in ("+pointIds+")").and("vpl.link_id=s.id")).query().getResultObj();

		List<Map> news = QBuilder.db("basedb").select("new map(n.newsId as news_id, n.title as title, vpl.link_type as link_type, vpl.point_id as point_id)").from("visit_point_link vpl, News n").where(new Where("vpl.link_type = 1").and("vpl.status = ", STATUS_ENABLE).and("vpl.point_id in ("+pointIds+")").and("vpl.link_id=n.id")).query().getResultObj();
		for (int i = 0; i < points.size(); i++) {
			Map point = points.get(i);
			String pointId = Tools.getDesc(point.get("point_id"), "-1");
			List<Map> lb = new ArrayList<Map>();
			// 加入量表
			Iterator<Map> ite = surveys.iterator();
			while (ite.hasNext()) {
				Map _map = ite.next();
				String _pointId = Tools.getDesc(_map.get("point_id"), "-2");
				if (pointId.equals(_pointId)) {
					lb.add(_map);
					ite.remove();
				}
			}

			// 加入新闻
			ite = news.iterator();
			while (ite.hasNext()) {
				Map _map = ite.next();
				String _pointId = Tools.getDesc(_map.get("point_id"), "-2");
				if (pointId.equals(_pointId)) {
					lb.add(_map);
					ite.remove();
				}
			}
			point.put("detail", lb);
		}

	}
	msg.put("data", template);
	Tools.encryptPrint(out, msg);
%>